﻿using UnityEditor;
using UnityEditor.UI;
using UnityEngine;

namespace CGF
{
    [CustomEditor(typeof(CLine))]
    public class CLineEditor : ImageEditor
    {
        private SerializedProperty _lineSizeProperty;
        private SerializedProperty _cornerSizeProperty;
        private SerializedProperty _pointsProperty;
        private SerializedProperty _useDashedLineProperty;
        private SerializedProperty _dashLengthProperty;
        private SerializedProperty _gapLengthProperty;

        protected override void OnEnable()
        {
            base.OnEnable();
            _lineSizeProperty = serializedObject.FindProperty("_lineSize");
            _cornerSizeProperty = serializedObject.FindProperty("_cornerSize");
            _pointsProperty = serializedObject.FindProperty("_points");
            _useDashedLineProperty = serializedObject.FindProperty("_useDashedLine");
            _dashLengthProperty = serializedObject.FindProperty("_dashLength");
            _gapLengthProperty = serializedObject.FindProperty("_gapLength");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            base.OnInspectorGUI();
            
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(_lineSizeProperty);
            EditorGUILayout.PropertyField(_cornerSizeProperty);
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(_pointsProperty, true);
            // 虚线选项
            EditorGUILayout.PropertyField(_useDashedLineProperty);
            if (_useDashedLineProperty.boolValue)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(_dashLengthProperty);
                EditorGUILayout.PropertyField(_gapLengthProperty);
                EditorGUI.indentLevel--;
            }
            serializedObject.ApplyModifiedProperties();
            // 强制重绘以立即看到效果
            if (GUI.changed)
            {
                CLine cline = target as CLine;
                if (cline != null)
                {
                    cline.SetVerticesDirty();
                }
            }
        }
    }
}